package com.hsc.lock.annotation;

import com.hsc.lock.ObjectUtlis;
import com.hsc.lock.interceptor.ReplayLockOperation;
import org.springframework.core.annotation.AnnotatedElementUtils;

public class DefaultReplayLockConfig extends ReplayLockOperation {


    private final Class<?> target;

    private boolean initialized = false;

    public DefaultReplayLockConfig(Class<?> target) {
        this.target = target;
    }

    public  void applyDefault(ReplayLockOperation lockOperation) {
        if (!this.initialized) {
            ReplayLockConfig annotation = AnnotatedElementUtils.findMergedAnnotation(this.target, ReplayLockConfig.class);
            if (annotation != null) {
                this.setKey(annotation.key());
                this.setKeyGenerator(annotation.keyGenerator());
                this.setKeySpace(annotation.keySpace());
                this.setKeyExpire(annotation.keyExpire());
                this.setRefuseMessage(annotation.refuseMessage());
                this.setRefuseHandler(annotation.refuseHandler());
            }
            this.initialized = true;
        }
        lockOperation.setKey(ObjectUtlis.defaultIfNull(lockOperation.getKey(), this.getKey()));
        lockOperation.setKeyGenerator(ObjectUtlis.defaultIfNull(lockOperation.getKeyGenerator(), this.getKeyGenerator()));
        lockOperation.setKeySpace(ObjectUtlis.defaultIfNull(lockOperation.getKeySpace(), this.getKeySpace()));
        lockOperation.setKeyExpire(ObjectUtlis.defaultIfNull(lockOperation.getKeyExpire(), this.getKeyExpire()));
        lockOperation.setRefuseMessage(ObjectUtlis.defaultIfNull(lockOperation.getRefuseMessage(), this.getRefuseMessage()));
        lockOperation.setRefuseHandler(ObjectUtlis.defaultIfNull(lockOperation.getRefuseHandler(), this.getRefuseHandler()));
    }


}
